package com._51cto.healthy.service;

import com._51cto.healthy.base.BaseMapper;
import com._51cto.healthy.content.P;
import com._51cto.healthy.mapper.CheckGroupMapper;
import com._51cto.healthy.pojo.CheckGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/****
 * @Author:DancingHorse
 * @Description:TCheckgroup业务层接口实现类
 * @Date 2022/4/14 0:16
 *****/
@DubboService
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper baseMapper;

    public BaseMapper<CheckGroup> getDao(){
        return baseMapper;
    }


    @Override
    public P<CheckGroup> queryPage(String query, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<CheckGroup> checkGroups = baseMapper.queryByCodeOrName(query);
        //把查询到的数据存入分页插件中
        PageInfo<CheckGroup> groupPageInfo = new PageInfo<>(checkGroups);
        return new P()
                .setPageSize(groupPageInfo.getPageSize())
                .setPageNum(groupPageInfo.getPageNum())
                .setPages(groupPageInfo.getPages())
                .setTotal(groupPageInfo.getTotal())
                .setRows(groupPageInfo.getList());
    }

    @Override
    public List<Integer> queryItemIds(Integer id) {
        return baseMapper.queryItemIds(id);
    }

    @Override
    public void deleteByPK(Integer pk) {
        baseMapper.deleteByPK(pk);
    }

    @Override
    @Transactional
    public void insert(CheckGroup checkGroup) {
        baseMapper.insert(checkGroup);
        //获取检测项ID并遍历
        checkGroup.getItemIds().forEach(itemIds->{
            //添加检测项ID
            baseMapper.insertItemId(checkGroup.getId(),itemIds);
        });
    }

    @Override
    @Transactional
    public void update(CheckGroup checkGroup) {
        //更新,删除,插入
        baseMapper.update(checkGroup);
        //删除检测项id根据检测组id
        baseMapper.deleteItemIdsByGroup(checkGroup.getId());
        //获取检测项ID并遍历
        checkGroup.getItemIds().forEach(itemId->{
            //添加检测项ID和检测组ID
            baseMapper.insertItemIdWithGroup(itemId,checkGroup.getId());
        });
    }
}